﻿using BattleBotsMattAndSunny.Components.Interfaces;
using BattleBotsMattAndSunny.Extensions;
using BattleBotsMattAndSunny.Model;
using Robocode;

namespace BattleBotsMattAndSunny.Components.Radar
{
    public class SweepRadar : IRadarManager
    {
        public AdvancedRobot robot { get; set; }

        public IDataManager dataManager { get; set; }

        public Enemy CurrentTarget { get; set; }

        public SweepRadar(IDataManager dm, AdvancedRobot r)
        {
            robot = r;
            dataManager = dm;
        }

        //todo add to abstract method?
        public void Scan(ScannedRobotEvent e)
        {
            dataManager.StoreScan(e);
            var existingEnemy = dataManager.GetEnemy(e.Name);
            if (existingEnemy == null)
            {
                var newEnemy = new Enemy(e, robot);
                dataManager.StoreEnemy(newEnemy);
            }
            else
            {
                existingEnemy.UpdateEnemy(e, robot);
            }
            //Focus on scanned enemy if it is closer
            if (CurrentTarget == null || CurrentTarget.Distance > e.Distance)
            {
                CurrentTarget = dataManager.GetEnemy(e.Name);
            }
        }

        public void Move()
        {
            robot.SetTurnGunLeft(15);
            robot.SetTurnRadarLeft(15);
        }
    }
}